package com.soft.base.spring.util;


import com.google.common.base.Objects;
import com.xiaoleilu.hutool.util.RandomUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class Request {

    private static final String IP_UTILS_FLAG = ",";
    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
    private static final String LOCALHOST_IP1 = "127.0.0.1";

    public static HttpServletRequest request() {
        if ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() == null) {
            return null;
        }
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static String getToken(final String name) {
        HttpServletRequest request = request();
        if (null == request) {
            return null;
        }
        String token = request.getHeader(name);
        if (StrUtil.isEmpty(token)) {
            Cookie[] cookies = request.getCookies();
            if (null != cookies) {
                for (Cookie cookie : cookies) {
                    if (Objects.equal(cookie.getName(), name)) {
                        return StrUtil.trim(cookie.getValue());
                    }
                }
            }
        }
        return token;
    }

    public static String generateToken() {
        return RandomUtil.randomString(64);
    }

    public static boolean isAjax() {
        HttpServletRequest reqeust = request();
        if (null == reqeust) {
            return false;
        }
        return "XMLHttpRequest".equalsIgnoreCase(reqeust.getHeader("x-requested-with"));
    }

    public static String url() {
        HttpServletRequest request = request();
        if (null == request) {
            return null;
        }
        return request.getServletPath();
    }

    public static String getAttributeString(String name) {
        HttpServletRequest request = request();
        if (null == request) {
            return null;
        }
        return (String) request.getAttribute(name);
    }

    public static void setAttribute(String name, Object value) {
        HttpServletRequest request = request();
        if (null == request) {
            return;
        }
        request.setAttribute(name, value);
    }

    public static String ip() {
        HttpServletRequest request = request();
        String ip = null;
        try {
            ip = request.getHeader("X-Original-Forwarded-For");
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("x-forwarded-for");
            }
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StrUtil.isEmpty(ip) || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            //兼容k8s集群获取ip
            if (StrUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                if (LOCALHOST_IP1.equalsIgnoreCase(ip) || LOCALHOST_IP.equalsIgnoreCase(ip)) {
                    //根据网卡取本机配置的IP
                    InetAddress iNet = null;
                    try {
                        iNet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                    }
                    ip = iNet.getHostAddress();
                }
            }
        } catch (Exception e) {
        }
        //使用代理，则获取第一个IP地址
        if (!StrUtil.isEmpty(ip) && ip.indexOf(IP_UTILS_FLAG) > 0) {
            ip = ip.substring(0, ip.indexOf(IP_UTILS_FLAG));
        }

        return ip;
    }
}
